<template>
  <div class="collect">
    <div class="top">
      <van-nav-bar left-arrow title="收藏列表" @click-left="$router.back()" />
    </div>


    <commonList :API="getFavList" :params="{ user_id: uid }" :refresh="true">
      <template #default="{ list }">
        <div class="main" v-for="item in list" :key="item._id">
          <van-swipe-cell>
            <div class="m-box">
              <div class="m-img">
                <van-image width="100" height="100" v-for="img in item.imageSrc" :key="img._id" :src="img" />
              </div>
              <div class="m-text">
                <div class="title">
                  <span>{{ item.title }}</span>
                </div>
                <div class="content"></div>
                <div class="footer">
                  <span>作者:{{ item.author }}</span>
                  <em>时间:{{ item.time | conversion }}</em>
                </div>
              </div>
              <div class="null">666</div>
            </div>
            <template #right>
              <van-button square text="删除" type="danger" class="delete-button" @click="favList(item._id)" />
            </template>
          </van-swipe-cell>
        </div>
      </template>
    </commonList>
  </div>

</template>

<script>
import { SwipeCell } from 'vant';
import { getFavList, removeFav, } from "@/api/api";
import { mapState } from "vuex";
import commonList from "@/components/commonList.vue";
export default {
  data() {
    return {

      getFavList
    };
  },
  components: {
    [commonList.name]: commonList,
    [SwipeCell.name]: SwipeCell,
  },
  created() {

  },
  computed: {
    ...mapState(["uid"]),
  },

  methods: {
    favList(article_id) {
      
      let { uid: user_id } = this

      removeFav({
        user_id,
        article_id
      }).then(res=>{
        console.log(res);
        if (res.code==0) {
          this.$toast(res.msg)
           this.$Bus.$emit('fav',article_id)

        }else{
          this.$toast(res.msg)
        }
      })
    }

  },
  filters: {
    //3.定义一个处理函数，参数value为要处理数据
    conversion: function (value) {
      //调用Date的方法，处理时间戳
      return new Date(value).toLocaleString();
    },
  },
};
</script>

<style lang="scss" lang="less" scoped>
.collect {
  background-color: rgb(245, 247, 249);
  height: 100vh;

  .main {

    margin-bottom: 5px;

    .m-box {

      background-color: #fff;
      padding: 10px;
      display: flex;
      justify-content: space-around;

      .m-img {
        width: 25%;
      }

      .m-text {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        width: 50%;

        em {
          font-style: normal;
          display: block;
        }
      }

      .null {
        width: 10%;
        display: flex;
        flex-direction: column-reverse;
      }

      .footer {
        span {
          margin-right: 10px;
        }
      }
    }
  }

}

.goods-card {
  margin: 0;
  margin-top: -10px;
  background-color: #fff;
}

.delete-button {
  height: 100%;
}
</style>